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

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

Issue 2694843003: Add field-initializing factory static methods to generated mojo unions. (Closed)
Patch Set: Created 3 years, 9 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 38d222b136995a93b668e607594989d1f2771b41..b1a78b2018df0cfe92dd0717b97073b6cbd0bfc7 100644
--- a/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_cpp_generator.py
@@ -4,6 +4,8 @@
"""Generates C++ source files from a mojom.Module."""
+import collections
+
import mojom.generate.generator as generator
import mojom.generate.module as mojom
import mojom.generate.pack as pack
@@ -179,6 +181,24 @@ def IsNativeOnlyKind(kind):
kind.native_only
+def IsDistinctUnionKind(union, field):
+ """Returns whether a field in a union has a distinct type from other fields.
+
+ The fields of a union for which this returns True may form an overload set
+ without ambiguity. Due to the way integral and floating point type promotion
+ works, integral and floating point types are considered not distinct.
+ """
+ if (mojom.IsIntegralKind(field.kind) or mojom.IsFloatKind(field.kind) or
+ mojom.IsDoubleKind(field.kind)):
+ return False
+
+ if not hasattr(union, 'field_types'):
+ union.field_types = collections.defaultdict(lambda: -1)
+ for union_field in union.fields:
+ union.field_types[GetCppWrapperParamType(union_field.kind)] += 1
+ return not union.field_types[
+ GetCppWrapperParamType(field.kind)]
+
def IsHashableKind(kind):
"""Check if the kind can be hashed.
@@ -638,6 +658,7 @@ class Generator(generator.Generator):
"is_any_interface_kind": mojom.IsAnyInterfaceKind,
"is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind,
"is_associated_kind": mojom.IsAssociatedKind,
+ "is_distinct_kind": IsDistinctUnionKind,
"is_hashable": IsHashableKind,
"is_map_kind": mojom.IsMapKind,
"is_nullable_kind": mojom.IsNullableKind,

Powered by Google App Engine
This is Rietveld 408576698