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

Side by Side 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 unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Generates C++ source files from a mojom.Module.""" 5 """Generates C++ source files from a mojom.Module."""
6 6
7 import collections
8
7 import mojom.generate.generator as generator 9 import mojom.generate.generator as generator
8 import mojom.generate.module as mojom 10 import mojom.generate.module as mojom
9 import mojom.generate.pack as pack 11 import mojom.generate.pack as pack
10 from mojom.generate.template_expander import UseJinja 12 from mojom.generate.template_expander import UseJinja
11 13
12 14
13 _kind_to_cpp_type = { 15 _kind_to_cpp_type = {
14 mojom.BOOL: "bool", 16 mojom.BOOL: "bool",
15 mojom.INT8: "int8_t", 17 mojom.INT8: "int8_t",
16 mojom.UINT8: "uint8_t", 18 mojom.UINT8: "uint8_t",
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 174
173 def IsTypemappedKind(kind): 175 def IsTypemappedKind(kind):
174 return hasattr(kind, "name") and \ 176 return hasattr(kind, "name") and \
175 GetFullMojomNameForKind(kind) in _current_typemap 177 GetFullMojomNameForKind(kind) in _current_typemap
176 178
177 def IsNativeOnlyKind(kind): 179 def IsNativeOnlyKind(kind):
178 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ 180 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \
179 kind.native_only 181 kind.native_only
180 182
181 183
184 def IsDistinctUnionKind(union, field):
185 """Returns whether a field in a union has a distinct type from other fields.
186
187 The fields of a union for which this returns True may form an overload set
188 without ambiguity. Due to the way integral and floating point type promotion
189 works, integral and floating point types are considered not distinct.
190 """
191 if (mojom.IsIntegralKind(field.kind) or mojom.IsFloatKind(field.kind) or
192 mojom.IsDoubleKind(field.kind)):
193 return False
194
195 if not hasattr(union, 'field_types'):
196 union.field_types = collections.defaultdict(lambda: -1)
197 for union_field in union.fields:
198 union.field_types[GetCppWrapperParamType(union_field.kind)] += 1
199 return not union.field_types[
200 GetCppWrapperParamType(field.kind)]
201
182 def IsHashableKind(kind): 202 def IsHashableKind(kind):
183 """Check if the kind can be hashed. 203 """Check if the kind can be hashed.
184 204
185 Args: 205 Args:
186 kind: {Kind} The kind to check. 206 kind: {Kind} The kind to check.
187 207
188 Returns: 208 Returns:
189 {bool} True if a value of this kind can be hashed. 209 {bool} True if a value of this kind can be hashed.
190 """ 210 """
191 checked = set() 211 checked = set()
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
631 "should_inline": ShouldInlineStruct, 651 "should_inline": ShouldInlineStruct,
632 "should_inline_union": ShouldInlineUnion, 652 "should_inline_union": ShouldInlineUnion,
633 "is_array_kind": mojom.IsArrayKind, 653 "is_array_kind": mojom.IsArrayKind,
634 "is_enum_kind": mojom.IsEnumKind, 654 "is_enum_kind": mojom.IsEnumKind,
635 "is_integral_kind": mojom.IsIntegralKind, 655 "is_integral_kind": mojom.IsIntegralKind,
636 "is_native_only_kind": IsNativeOnlyKind, 656 "is_native_only_kind": IsNativeOnlyKind,
637 "is_any_handle_kind": mojom.IsAnyHandleKind, 657 "is_any_handle_kind": mojom.IsAnyHandleKind,
638 "is_any_interface_kind": mojom.IsAnyInterfaceKind, 658 "is_any_interface_kind": mojom.IsAnyInterfaceKind,
639 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, 659 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind,
640 "is_associated_kind": mojom.IsAssociatedKind, 660 "is_associated_kind": mojom.IsAssociatedKind,
661 "is_distinct_kind": IsDistinctUnionKind,
641 "is_hashable": IsHashableKind, 662 "is_hashable": IsHashableKind,
642 "is_map_kind": mojom.IsMapKind, 663 "is_map_kind": mojom.IsMapKind,
643 "is_nullable_kind": mojom.IsNullableKind, 664 "is_nullable_kind": mojom.IsNullableKind,
644 "is_object_kind": mojom.IsObjectKind, 665 "is_object_kind": mojom.IsObjectKind,
645 "is_reference_kind": mojom.IsReferenceKind, 666 "is_reference_kind": mojom.IsReferenceKind,
646 "is_string_kind": mojom.IsStringKind, 667 "is_string_kind": mojom.IsStringKind,
647 "is_struct_kind": mojom.IsStructKind, 668 "is_struct_kind": mojom.IsStructKind,
648 "is_typemapped_kind": IsTypemappedKind, 669 "is_typemapped_kind": IsTypemappedKind,
649 "is_union_kind": mojom.IsUnionKind, 670 "is_union_kind": mojom.IsUnionKind,
650 "passes_associated_kinds": mojom.PassesAssociatedKinds, 671 "passes_associated_kinds": mojom.PassesAssociatedKinds,
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
809 global _variant 830 global _variant
810 _variant = self.variant 831 _variant = self.variant
811 global _export_attribute 832 global _export_attribute
812 _export_attribute = self.export_attribute 833 _export_attribute = self.export_attribute
813 suffix = "-%s" % self.variant if self.variant else "" 834 suffix = "-%s" % self.variant if self.variant else ""
814 self.Write(self.GenerateModuleHeader(), 835 self.Write(self.GenerateModuleHeader(),
815 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) 836 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix)))
816 self.Write( 837 self.Write(
817 self.GenerateModuleSource(), 838 self.GenerateModuleSource(),
818 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) 839 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix)))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698