Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(708)

Unified Diff: mojo/public/tools/bindings/generators/mojom_cpp_generator.py

Issue 2339413004: Allow Mojo structs as map keys (Closed)
Patch Set: Fix hash unit test on Windows Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 de906690344d6b1db2664376002d81b77bae5c6a..07eb45bfc3ec2681a844efcd9b5295fe3abc7456 100644
--- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
@@ -152,9 +152,11 @@ def DefaultValue(field):
def NamespaceToArray(namespace):
return namespace.split(".") if namespace else []
-def GetNameForKind(kind, internal=False, flatten_nested_kind=False):
+def GetNameForKind(kind, internal=False, flatten_nested_kind=False,
+ add_same_module_namespaces=False):
return _NameFormatter(kind, _variant).FormatForCpp(
- internal=internal, flatten_nested_kind=flatten_nested_kind)
+ internal=internal, flatten_nested_kind=flatten_nested_kind,
+ add_same_module_namespaces=add_same_module_namespaces)
def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False):
return _NameFormatter(kind, _variant).FormatForCpp(
@@ -172,6 +174,45 @@ def IsNativeOnlyKind(kind):
return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \
kind.native_only
+
+def IsHashableKind(kind):
+ """Check if the kind can be hashed.
+
+ Args:
+ kind: {Kind} The kind to check.
+
+ Returns:
+ {bool} True if a value of this kind can be hashed.
+ """
+ checked = set()
+ def Check(kind):
+ if kind.spec in checked:
+ return True
+ checked.add(kind.spec)
+ if mojom.IsNullableKind(kind):
+ return False
+ elif mojom.IsStructKind(kind):
+ if (IsTypemappedKind(kind) and
+ not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"]):
+ return False
+ return all(Check(field.kind) for field in kind.fields)
+ elif mojom.IsUnionKind(kind):
+ return all(Check(field.kind) for field in kind.fields)
+ elif mojom.IsAnyHandleKind(kind):
+ return False
+ elif mojom.IsAnyInterfaceKind(kind):
+ return False
+ # TODO(tibell): Arrays and maps could be made hashable. We just don't have a
+ # use case yet.
+ elif mojom.IsArrayKind(kind):
+ return False
+ elif mojom.IsMapKind(kind):
+ return False
+ else:
+ return True
+ return Check(kind)
+
+
def GetNativeTypeName(typemapped_kind):
return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"]
@@ -180,7 +221,7 @@ def GetCppPodType(kind):
return "char*"
return _kind_to_cpp_type[kind]
-def GetCppWrapperType(kind):
+def GetCppWrapperType(kind, add_same_module_namespaces=False):
def _AddOptional(type_name):
pattern = "WTF::Optional<%s>" if _for_blink else "base::Optional<%s>"
return pattern % type_name
@@ -193,9 +234,11 @@ def GetCppWrapperType(kind):
type_name = _AddOptional(type_name)
return type_name
if mojom.IsEnumKind(kind):
- return GetNameForKind(kind)
+ return GetNameForKind(
+ kind, add_same_module_namespaces=add_same_module_namespaces)
if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
- return "%sPtr" % GetNameForKind(kind)
+ return "%sPtr" % GetNameForKind(
+ kind, add_same_module_namespaces=add_same_module_namespaces)
if mojom.IsArrayKind(kind):
pattern = None
if _use_new_wrapper_types:
@@ -204,7 +247,8 @@ def GetCppWrapperType(kind):
pattern = _AddOptional(pattern)
else:
pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>"
- return pattern % GetCppWrapperType(kind.kind)
+ return pattern % GetCppWrapperType(
+ kind.kind, add_same_module_namespaces=add_same_module_namespaces)
if mojom.IsMapKind(kind):
pattern = None
if _use_new_wrapper_types:
@@ -214,16 +258,25 @@ def GetCppWrapperType(kind):
pattern = _AddOptional(pattern)
else:
pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>"
- return pattern % (GetCppWrapperType(kind.key_kind),
- GetCppWrapperType(kind.value_kind))
+ return pattern % (
+ GetCppWrapperType(
+ kind.key_kind,
+ add_same_module_namespaces=add_same_module_namespaces),
+ GetCppWrapperType(
+ kind.value_kind,
+ add_same_module_namespaces=add_same_module_namespaces))
if mojom.IsInterfaceKind(kind):
- return "%sPtr" % GetNameForKind(kind)
+ return "%sPtr" % GetNameForKind(
+ kind, add_same_module_namespaces=add_same_module_namespaces)
if mojom.IsInterfaceRequestKind(kind):
- return "%sRequest" % GetNameForKind(kind.kind)
+ return "%sRequest" % GetNameForKind(
+ kind.kind, add_same_module_namespaces=add_same_module_namespaces)
if mojom.IsAssociatedInterfaceKind(kind):
- return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind)
+ return "%sAssociatedPtrInfo" % GetNameForKind(
+ kind.kind, add_same_module_namespaces=add_same_module_namespaces)
if mojom.IsAssociatedInterfaceRequestKind(kind):
- return "%sAssociatedRequest" % GetNameForKind(kind.kind)
+ return "%sAssociatedRequest" % GetNameForKind(
+ kind.kind, add_same_module_namespaces=add_same_module_namespaces)
if mojom.IsStringKind(kind):
if _for_blink:
return "WTF::String"
@@ -313,6 +366,22 @@ def GetUnionGetterReturnType(kind):
return "%s&" % GetCppWrapperType(kind)
return GetCppWrapperType(kind)
+def GetUnionTraitGetterReturnType(kind):
+ """Get field type used in UnionTraits template specialization.
+
+ The type may be qualified as UnionTraits specializations live outside the
+ namespace where e.g. structs are defined.
+
+ Args:
+ kind: {Kind} The type of the field.
+
+ Returns:
+ {str} The C++ type to use for the field.
+ """
+ if mojom.IsReferenceKind(kind):
+ return "%s&" % GetCppWrapperType(kind, add_same_module_namespaces=True)
+ return GetCppWrapperType(kind, add_same_module_namespaces=True)
+
def GetCppDataViewType(kind, qualified=False):
def _GetName(input_kind):
return _NameFormatter(input_kind, None).FormatForCpp(
@@ -465,6 +534,7 @@ class Generator(generator.Generator):
cpp_filters = {
"constant_value": ConstantValue,
+ "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces,
"contains_move_only_members": ContainsMoveOnlyMembers,
"cpp_wrapper_param_type": GetCppWrapperParamType,
"cpp_data_view_type": GetCppDataViewType,
@@ -472,6 +542,7 @@ class Generator(generator.Generator):
"cpp_union_field_type": GetCppUnionFieldType,
"cpp_pod_type": GetCppPodType,
"cpp_union_getter_return_type": GetUnionGetterReturnType,
+ "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType,
"cpp_wrapper_type": GetCppWrapperType,
"default_value": DefaultValue,
"expression_to_text": ExpressionToText,
@@ -493,9 +564,11 @@ class Generator(generator.Generator):
"is_any_interface_kind": mojom.IsAnyInterfaceKind,
"is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind,
"is_associated_kind": mojom.IsAssociatedKind,
+ "is_hashable": IsHashableKind,
"is_map_kind": mojom.IsMapKind,
"is_nullable_kind": mojom.IsNullableKind,
"is_object_kind": mojom.IsObjectKind,
+ "is_reference_kind": mojom.IsReferenceKind,
"is_string_kind": mojom.IsStringKind,
"is_struct_kind": mojom.IsStructKind,
"is_typemapped_kind": IsTypemappedKind,

Powered by Google App Engine
This is Rietveld 408576698