Chromium Code Reviews| 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 e66bec38bd32749d604981de050d1c91755535ec..84e47f469e363b5d93277fe06373820ba18b6b74 100644 |
| --- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py |
| +++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py |
| @@ -191,10 +191,8 @@ def IsHashableKind(kind): |
| 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) or _current_typemap[ |
| + GetFullMojomNameForKind(kind)]["hashable"] |
| elif mojom.IsUnionKind(kind): |
| return all(Check(field.kind) for field in kind.fields) |
| elif mojom.IsAnyHandleKind(kind): |
| @@ -419,8 +417,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": |
| @@ -575,7 +572,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 +579,81 @@ 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 sorted(extra_headers) |
| + |
| + def _GetAllUsedTypemaps(self): |
| + """Returns the typemaps for types needed for serialization in this module. |
| + |
| + A type is needed for serialization if it is contained by a struct or enum |
|
yzshen1
2017/02/07 00:21:45
enum -> union?
Sam McNally
2017/02/07 01:48:31
Done.
|
| + defined in this module, is a parameter of a message in an interface in |
| + this module or is contained within another type needed for serialization. |
| + """ |
| + 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 = GetFullMojomNameForKind(kind) |
| + 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 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) |
| + |
| + types = set(GetFullMojomNameForKind(typename) |
| + for typename in |
| + self.module.structs + all_enums + self.module.unions) |
| + headers = set() |
| + for typename, typemap in self.typemap.iteritems(): |
| + if typename in types: |
| + headers.update(typemap.get("public_headers", [])) |
| + return sorted(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() |