| Index: mojo/public/tools/bindings/generators/mojom_cpp_generator.py
|
| diff --git a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
|
| index be70d2f8fc577f657a072c35fb7525c68797568e..710b05a67120e21cc881e25e18d2cfa1d61c108e 100644
|
| --- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
|
| +++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
|
| @@ -41,12 +41,7 @@ _for_blink = False
|
| _variant = None
|
|
|
|
|
| -class _NameFormatter(object):
|
| - """A formatter for the names of kinds or values."""
|
| -
|
| - def __init__(self, token, variant):
|
| - self._token = token
|
| - self._variant = variant
|
| +class _NameFormatter(generator.NameFormatter):
|
|
|
| def Format(self, separator, prefixed=False, internal=False,
|
| include_variant=False, add_same_module_namespaces=False,
|
| @@ -84,49 +79,20 @@ class _NameFormatter(object):
|
| internal=internal, include_variant=True,
|
| flatten_nested_kind=flatten_nested_kind)
|
|
|
| - def FormatForMojom(self):
|
| - return self.Format(".", add_same_module_namespaces=True)
|
| -
|
| - def _MapKindName(self, token, internal):
|
| + def _MapKindName(self, token, internal=False):
|
| if not internal:
|
| - return token.name
|
| + return super(_NameFormatter, self)._MapKindName(token)
|
| if (mojom.IsStructKind(token) or mojom.IsUnionKind(token) or
|
| mojom.IsEnumKind(token)):
|
| return token.name + "_Data"
|
| - return token.name
|
| + return super(_NameFormatter, self)._MapKindName(token)
|
|
|
| def _GetName(self, internal, flatten_nested_kind):
|
| - if isinstance(self._token, mojom.EnumValue):
|
| - name_parts = _NameFormatter(self._token.enum, self._variant)._GetName(
|
| - internal, flatten_nested_kind)
|
| - name_parts.append(self._token.name)
|
| - return name_parts
|
| -
|
| - name_parts = []
|
| + name = super(_NameFormatter, self)._GetName(
|
| + internal=internal, flatten_nested_kind=flatten_nested_kind)
|
| if internal:
|
| - name_parts.append("internal")
|
| -
|
| - if (flatten_nested_kind and mojom.IsEnumKind(self._token) and
|
| - self._token.parent_kind):
|
| - name = "%s_%s" % (self._token.parent_kind.name,
|
| - self._MapKindName(self._token, internal))
|
| - name_parts.append(name)
|
| - return name_parts
|
| -
|
| - if self._token.parent_kind:
|
| - name_parts.append(self._MapKindName(self._token.parent_kind, internal))
|
| - name_parts.append(self._MapKindName(self._token, internal))
|
| - return name_parts
|
| -
|
| - def _ShouldIncludeNamespace(self, add_same_module_namespaces):
|
| - return add_same_module_namespaces or self._token.imported_from
|
| -
|
| - def _GetNamespace(self):
|
| - if self._token.imported_from:
|
| - return NamespaceToArray(self._token.imported_from["namespace"])
|
| - elif hasattr(self._token, "module"):
|
| - return NamespaceToArray(self._token.module.namespace)
|
| - return []
|
| + name.insert(0, 'internal')
|
| + return name
|
|
|
|
|
| def ConstantValue(constant):
|
| @@ -157,12 +123,9 @@ def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False):
|
| internal=internal, add_same_module_namespaces=True,
|
| flatten_nested_kind=flatten_nested_kind)
|
|
|
| -def GetFullMojomNameForKind(kind):
|
| - return _NameFormatter(kind, _variant).FormatForMojom()
|
| -
|
| def IsTypemappedKind(kind):
|
| return hasattr(kind, "name") and \
|
| - GetFullMojomNameForKind(kind) in _current_typemap
|
| + generator.GetFullMojomNameForKind(kind) in _current_typemap
|
|
|
| def IsNativeOnlyKind(kind):
|
| return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \
|
| @@ -186,15 +149,13 @@ def IsHashableKind(kind):
|
| if mojom.IsNullableKind(kind):
|
| return False
|
| elif mojom.IsStructKind(kind):
|
| - if (IsTypemappedKind(kind) and
|
| - not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"]):
|
| + if (IsTypemappedKind(kind) and not _current_typemap[
|
| + generator.GetFullMojomNameForKind(kind)]["hashable"]):
|
| return False
|
| return all(Check(field.kind) for field in kind.fields)
|
| elif mojom.IsEnumKind(kind):
|
| - if (IsTypemappedKind(kind) and
|
| - not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"]):
|
| - return False
|
| - return True
|
| + return not (IsTypemappedKind(kind) and not _current_typemap[
|
| + generator.GetFullMojomNameForKind(kind)]["hashable"])
|
| elif mojom.IsUnionKind(kind):
|
| return all(Check(field.kind) for field in kind.fields)
|
| elif mojom.IsAnyHandleKind(kind):
|
| @@ -213,7 +174,8 @@ def IsHashableKind(kind):
|
|
|
|
|
| def GetNativeTypeName(typemapped_kind):
|
| - return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"]
|
| + return _current_typemap[
|
| + generator.GetFullMojomNameForKind(typemapped_kind)]["typename"]
|
|
|
| def GetCppPodType(kind):
|
| return _kind_to_cpp_type[kind]
|
| @@ -234,7 +196,7 @@ def GetCppWrapperType(kind, add_same_module_namespaces=False):
|
| if IsTypemappedKind(kind):
|
| type_name = GetNativeTypeName(kind)
|
| if (mojom.IsNullableKind(kind) and
|
| - not _current_typemap[GetFullMojomNameForKind(kind)][
|
| + not _current_typemap[generator.GetFullMojomNameForKind(kind)][
|
| "nullable_is_same_type"]):
|
| type_name = _AddOptional(type_name)
|
| return type_name
|
| @@ -297,7 +259,8 @@ def IsMoveOnlyKind(kind):
|
| if IsTypemappedKind(kind):
|
| if mojom.IsEnumKind(kind):
|
| return False
|
| - return _current_typemap[GetFullMojomNameForKind(kind)]["move_only"]
|
| + return _current_typemap[
|
| + generator.GetFullMojomNameForKind(kind)]["move_only"]
|
| if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
|
| return True
|
| if mojom.IsArrayKind(kind):
|
| @@ -311,7 +274,7 @@ def IsMoveOnlyKind(kind):
|
| def IsCopyablePassByValue(kind):
|
| if not IsTypemappedKind(kind):
|
| return False
|
| - return _current_typemap[GetFullMojomNameForKind(kind)][
|
| + return _current_typemap[generator.GetFullMojomNameForKind(kind)][
|
| "copyable_pass_by_value"]
|
|
|
| def ShouldPassParamByValue(kind):
|
| @@ -419,8 +382,7 @@ def GetUnmappedTypeForSerializer(kind):
|
|
|
| def TranslateConstants(token, kind):
|
| if isinstance(token, mojom.NamedValue):
|
| - return _NameFormatter(token, _variant).FormatForCpp(
|
| - flatten_nested_kind=True)
|
| + return GetNameForKind(token, flatten_nested_kind=True)
|
|
|
| if isinstance(token, mojom.BuiltinValue):
|
| if token.value == "double.INFINITY":
|
| @@ -530,6 +492,91 @@ def GetNewContainerValidateParams(kind):
|
| return "new mojo::internal::ContainerValidateParams(%s)" % (
|
| GetContainerValidateParamsCtorArgs(kind))
|
|
|
| +
|
| +def QuoteHeader(header):
|
| + if header[0] == '<':
|
| + return header
|
| + return '"%s"' % header
|
| +
|
| +
|
| +def QuoteAndSortHeaders(headers):
|
| + return (QuoteHeader(header) for header in sorted(headers))
|
| +
|
| +
|
| +CHROMIUM_HEADERS = {
|
| + "array": ["<vector>"],
|
| + "interface": [
|
| + "base/callback.h",
|
| + "mojo/public/cpp/bindings/associated_interface_ptr.h",
|
| + "mojo/public/cpp/bindings/associated_interface_ptr_info.h",
|
| + "mojo/public/cpp/bindings/associated_interface_request.h",
|
| + "mojo/public/cpp/bindings/interface_ptr.h",
|
| + "mojo/public/cpp/bindings/interface_request.h",
|
| + "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h",
|
| + "mojo/public/cpp/bindings/thread_safe_interface_ptr.h",
|
| + ],
|
| + "map": ["<unordered_map>"],
|
| + "native_enum": ["mojo/public/cpp/bindings/native_enum.h"],
|
| + "native_struct": ["mojo/public/cpp/bindings/native_struct.h"],
|
| + "optional": ["base/optional.h"],
|
| + "string": ["<string>"],
|
| + "struct": [
|
| + "<vector>",
|
| + "mojo/public/cpp/bindings/lib/clone_equals_util.h",
|
| + "mojo/public/cpp/bindings/lib/hash_util.h",
|
| + "mojo/public/cpp/bindings/struct_ptr.h",
|
| + "mojo/public/cpp/bindings/struct_traits.h",
|
| + ],
|
| + "union": [
|
| + "<vector>",
|
| + "mojo/public/cpp/bindings/lib/union_accessor.h",
|
| + "mojo/public/cpp/bindings/union_traits.h",
|
| + ],
|
| +}
|
| +
|
| +BLINK_HEADERS = {
|
| + "array": ["third_party/WebKit/Source/wtf/Vector.h"],
|
| + "interface": [
|
| + "base/callback.h",
|
| + "mojo/public/cpp/bindings/associated_interface_ptr.h",
|
| + "mojo/public/cpp/bindings/associated_interface_ptr_info.h",
|
| + "mojo/public/cpp/bindings/associated_interface_request.h",
|
| + "mojo/public/cpp/bindings/interface_ptr.h",
|
| + "mojo/public/cpp/bindings/interface_request.h",
|
| + "mojo/public/cpp/bindings/raw_ptr_impl_ref_traits.h",
|
| + "mojo/public/cpp/bindings/thread_safe_interface_ptr.h",
|
| + ],
|
| + "map": ["third_party/WebKit/Source/wtf/HashMap.h"],
|
| + "native_enum": ["mojo/public/cpp/bindings/native_enum.h"],
|
| + "native_struct": ["mojo/public/cpp/bindings/native_struct.h"],
|
| + "optional": ["third_party/WebKit/Source/wtf/Optional.h"],
|
| + "string": ["third_party/WebKit/Source/wtf/text/WTFString.h"],
|
| + "struct": [
|
| + "third_party/WebKit/Source/wtf/Vector.h",
|
| + "mojo/public/cpp/bindings/struct_ptr.h",
|
| + "mojo/public/cpp/bindings/struct_traits.h",
|
| + "mojo/public/cpp/bindings/lib/wtf_clone_equals_util.h",
|
| + "mojo/public/cpp/bindings/lib/wtf_hash_util.h",
|
| + ],
|
| + "union": [
|
| + "mojo/public/cpp/bindings/lib/union_accessor.h",
|
| + "mojo/public/cpp/bindings/union_traits.h",
|
| + "third_party/WebKit/Source/wtf/Vector.h",
|
| + ],
|
| +}
|
| +
|
| +SHARED_HEADERS = {
|
| + "array": ["mojo/public/cpp/bindings/array_data_view.h"],
|
| + "enum": ["mojo/public/cpp/bindings/enum_traits.h"],
|
| + "interface": ["mojo/public/cpp/bindings/interface_data_view.h"],
|
| + "map": ["mojo/public/cpp/bindings/map_data_view.h"],
|
| + "native_enum": ["mojo/public/cpp/bindings/native_enum.h"],
|
| + "native_struct": ["mojo/public/cpp/bindings/native_struct_data_view.h"],
|
| + "non_native_enum": ["<ostream>"],
|
| + "string": ["mojo/public/cpp/bindings/string_data_view.h"],
|
| +}
|
| +
|
| +
|
| class Generator(generator.Generator):
|
|
|
| cpp_filters = {
|
| @@ -575,7 +622,6 @@ class Generator(generator.Generator):
|
| "is_typemapped_kind": IsTypemappedKind,
|
| "is_union_kind": mojom.IsUnionKind,
|
| "passes_associated_kinds": mojom.PassesAssociatedKinds,
|
| - "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE,
|
| "stylize_method": generator.StudlyCapsToCamel,
|
| "under_to_camel": generator.UnderToCamel,
|
| "unmapped_type_for_serializer": GetUnmappedTypeForSerializer,
|
| @@ -583,15 +629,113 @@ class Generator(generator.Generator):
|
|
|
| def GetExtraTraitsHeaders(self):
|
| extra_headers = set()
|
| - for entry in self.typemap.itervalues():
|
| - extra_headers.update(entry.get("traits_headers", []))
|
| - return list(extra_headers)
|
| + for typemap in self._GetAllUsedTypemaps():
|
| + extra_headers.update(typemap.get("traits_headers", []))
|
| + return QuoteAndSortHeaders(extra_headers)
|
| +
|
| + def _GetAllUsedTypemaps(self):
|
| + used_typemaps = []
|
| + seen_types = set()
|
| + def AddKind(kind):
|
| + if (mojom.IsIntegralKind(kind) or mojom.IsStringKind(kind) or
|
| + mojom.IsDoubleKind(kind) or mojom.IsFloatKind(kind) or
|
| + mojom.IsAnyHandleKind(kind) or
|
| + mojom.IsInterfaceKind(kind) or
|
| + mojom.IsInterfaceRequestKind(kind) or
|
| + mojom.IsAssociatedKind(kind)):
|
| + pass
|
| + elif mojom.IsArrayKind(kind):
|
| + AddKind(kind.kind)
|
| + elif mojom.IsMapKind(kind):
|
| + AddKind(kind.key_kind)
|
| + AddKind(kind.value_kind)
|
| + else:
|
| + name = generator.GetFullMojomNameForKind(kind, self.variant)
|
| + if name in seen_types:
|
| + return
|
| + seen_types.add(name)
|
| +
|
| + typemap = _current_typemap.get(name, None)
|
| + if typemap:
|
| + used_typemaps.append(typemap)
|
| + if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
|
| + for field in kind.fields:
|
| + AddKind(field.kind)
|
| +
|
| + for kind in self.module.structs + self.module.unions:
|
| + for field in kind.fields:
|
| + AddKind(field.kind)
|
| +
|
| + for interface in self.module.interfaces:
|
| + for method in interface.methods:
|
| + for parameter in method.parameters + (method.response_parameters or []):
|
| + AddKind(parameter.kind)
|
| +
|
| + return used_typemaps
|
| +
|
| + def _GetPublicHeadersImpl(self, header_type_to_headers):
|
| + header_types = set()
|
| + headers = set()
|
| +
|
| + if self.module.structs:
|
| + header_types.add('struct')
|
| + if self.module.unions:
|
| + header_types.add('union')
|
| + if self.module.interfaces:
|
| + header_types.add('interface')
|
|
|
| - def GetExtraPublicHeaders(self):
|
| - extra_headers = set()
|
| - for entry in self.typemap.itervalues():
|
| - extra_headers.update(entry.get("public_headers", []))
|
| - return list(extra_headers)
|
| + all_enums = list(self.module.enums)
|
| + for struct in self.module.structs:
|
| + all_enums.extend(struct.enums)
|
| + for interface in self.module.interfaces:
|
| + all_enums.extend(interface.enums)
|
| +
|
| + potential_types = set(
|
| + generator.GetFullMojomNameForKind(typename, self.variant)
|
| + for typename in
|
| + self.module.structs + all_enums + self.module.unions)
|
| + for key, entry in self.typemap.iteritems():
|
| + if key in potential_types:
|
| + headers.update(entry.get("public_headers", []))
|
| + if all_enums:
|
| + header_types.add('enum')
|
| + native_enums = [enum.native_only for enum in all_enums]
|
| + if native_enums:
|
| + header_types.add('native_enum')
|
| + if len(native_enums) != len(all_enums):
|
| + header_types.add('non_native_enum')
|
| +
|
| + if any(struct.native_only for struct in self.module.structs):
|
| + header_types.add('native_struct')
|
| + for kind in self._GetDirectlyUsedKinds():
|
| + if hasattr(kind, 'is_nullable') and kind.is_nullable:
|
| + header_types.add('optional')
|
| + if mojom.IsStringKind(kind):
|
| + header_types.add('string')
|
| + elif mojom.IsArrayKind(kind):
|
| + header_types.add('array')
|
| + elif mojom.IsMapKind(kind):
|
| + header_types.add('map')
|
| + for header_type in header_types:
|
| + headers.update(header_type_to_headers.get(header_type, []))
|
| + return QuoteAndSortHeaders(headers)
|
| +
|
| + def GetPublicHeaders(self):
|
| + return self._GetPublicHeadersImpl(
|
| + BLINK_HEADERS if self.for_blink else CHROMIUM_HEADERS)
|
| +
|
| + def GetPublicSharedHeaders(self):
|
| + return self._GetPublicHeadersImpl(SHARED_HEADERS)
|
| +
|
| + def _GetDirectlyUsedKinds(self):
|
| + for struct in self.module.structs + self.module.unions:
|
| + for field in struct.fields:
|
| + yield field.kind
|
| +
|
| + for interface in self.module.interfaces:
|
| + for method in interface.methods:
|
| + for param in method.parameters + (method.response_parameters or []):
|
| + yield param.kind
|
|
|
| def GetJinjaExports(self):
|
| structs = self.GetStructs()
|
| @@ -605,7 +749,7 @@ class Generator(generator.Generator):
|
| return {
|
| "module": self.module,
|
| "namespace": self.module.namespace,
|
| - "namespaces_as_array": NamespaceToArray(self.module.namespace),
|
| + "namespaces_as_array": generator.NamespaceToArray(self.module.namespace),
|
| "imports": self.module.imports,
|
| "kinds": self.module.kinds,
|
| "enums": self.module.enums,
|
| @@ -615,7 +759,8 @@ class Generator(generator.Generator):
|
| "interfaces": interfaces,
|
| "variant": self.variant,
|
| "extra_traits_headers": self.GetExtraTraitsHeaders(),
|
| - "extra_public_headers": self.GetExtraPublicHeaders(),
|
| + "public_headers": self.GetPublicHeaders(),
|
| + "public_shared_headers": self.GetPublicSharedHeaders(),
|
| "for_blink": self.for_blink,
|
| "use_once_callback": self.use_once_callback,
|
| "export_attribute": self.export_attribute,
|
|
|