OLD | NEW |
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 mojom.generate.generator as generator | 7 import mojom.generate.generator as generator |
8 import mojom.generate.module as mojom | 8 import mojom.generate.module as mojom |
9 import mojom.generate.pack as pack | 9 import mojom.generate.pack as pack |
10 from mojom.generate.template_expander import UseJinja | 10 from mojom.generate.template_expander import UseJinja |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 return _NameFormatter(kind, _variant).FormatForMojom() | 165 return _NameFormatter(kind, _variant).FormatForMojom() |
166 | 166 |
167 def IsTypemappedKind(kind): | 167 def IsTypemappedKind(kind): |
168 return hasattr(kind, "name") and \ | 168 return hasattr(kind, "name") and \ |
169 GetFullMojomNameForKind(kind) in _current_typemap | 169 GetFullMojomNameForKind(kind) in _current_typemap |
170 | 170 |
171 def IsNativeOnlyKind(kind): | 171 def IsNativeOnlyKind(kind): |
172 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ | 172 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ |
173 kind.native_only | 173 kind.native_only |
174 | 174 |
| 175 |
| 176 def IsHashableKind(kind): |
| 177 """Check if the kind can be hashed. |
| 178 |
| 179 Args: |
| 180 kind: {Kind} The kind to check. |
| 181 |
| 182 Returns: |
| 183 {bool} True if a value of this kind can be hashed. |
| 184 """ |
| 185 checked = set() |
| 186 def Check(kind): |
| 187 if kind.spec in checked: |
| 188 return True |
| 189 checked.add(kind.spec) |
| 190 if mojom.IsNullableKind(kind): |
| 191 return False |
| 192 elif mojom.IsStructKind(kind): |
| 193 if (kind.native_only or |
| 194 (IsTypemappedKind(kind) and |
| 195 not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"])): |
| 196 return False |
| 197 return all(Check(field.kind) for field in kind.fields) |
| 198 elif mojom.IsUnionKind(kind): |
| 199 return all(Check(field.kind) for field in kind.fields) |
| 200 elif mojom.IsAnyHandleKind(kind): |
| 201 return False |
| 202 elif mojom.IsAnyInterfaceKind(kind): |
| 203 return False |
| 204 elif mojom.IsArrayKind(kind): |
| 205 return False |
| 206 elif mojom.IsMapKind(kind): |
| 207 return False |
| 208 else: |
| 209 return True |
| 210 return Check(kind) |
| 211 |
| 212 |
175 def GetNativeTypeName(typemapped_kind): | 213 def GetNativeTypeName(typemapped_kind): |
176 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] | 214 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] |
177 | 215 |
178 def GetCppPodType(kind): | 216 def GetCppPodType(kind): |
179 if mojom.IsStringKind(kind): | 217 if mojom.IsStringKind(kind): |
180 return "char*" | 218 return "char*" |
181 return _kind_to_cpp_type[kind] | 219 return _kind_to_cpp_type[kind] |
182 | 220 |
183 def GetCppWrapperType(kind): | 221 def GetCppWrapperType(kind): |
184 def _AddOptional(type_name): | 222 def _AddOptional(type_name): |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 not mojom.IsStringKind(kind)): | 496 not mojom.IsStringKind(kind)): |
459 return "nullptr" | 497 return "nullptr" |
460 | 498 |
461 return "new mojo::internal::ContainerValidateParams(%s)" % ( | 499 return "new mojo::internal::ContainerValidateParams(%s)" % ( |
462 GetContainerValidateParamsCtorArgs(kind)) | 500 GetContainerValidateParamsCtorArgs(kind)) |
463 | 501 |
464 class Generator(generator.Generator): | 502 class Generator(generator.Generator): |
465 | 503 |
466 cpp_filters = { | 504 cpp_filters = { |
467 "constant_value": ConstantValue, | 505 "constant_value": ConstantValue, |
| 506 "contains_handles": mojom.ContainsHandles, |
468 "contains_move_only_members": ContainsMoveOnlyMembers, | 507 "contains_move_only_members": ContainsMoveOnlyMembers, |
469 "cpp_wrapper_param_type": GetCppWrapperParamType, | 508 "cpp_wrapper_param_type": GetCppWrapperParamType, |
470 "cpp_data_view_type": GetCppDataViewType, | 509 "cpp_data_view_type": GetCppDataViewType, |
471 "cpp_field_type": GetCppFieldType, | 510 "cpp_field_type": GetCppFieldType, |
472 "cpp_union_field_type": GetCppUnionFieldType, | 511 "cpp_union_field_type": GetCppUnionFieldType, |
473 "cpp_pod_type": GetCppPodType, | 512 "cpp_pod_type": GetCppPodType, |
474 "cpp_union_getter_return_type": GetUnionGetterReturnType, | 513 "cpp_union_getter_return_type": GetUnionGetterReturnType, |
475 "cpp_wrapper_type": GetCppWrapperType, | 514 "cpp_wrapper_type": GetCppWrapperType, |
476 "default_value": DefaultValue, | 515 "default_value": DefaultValue, |
477 "expression_to_text": ExpressionToText, | 516 "expression_to_text": ExpressionToText, |
478 "get_container_validate_params_ctor_args": | 517 "get_container_validate_params_ctor_args": |
479 GetContainerValidateParamsCtorArgs, | 518 GetContainerValidateParamsCtorArgs, |
480 "get_name_for_kind": GetNameForKind, | 519 "get_name_for_kind": GetNameForKind, |
481 "get_pad": pack.GetPad, | 520 "get_pad": pack.GetPad, |
482 "get_qualified_name_for_kind": GetQualifiedNameForKind, | 521 "get_qualified_name_for_kind": GetQualifiedNameForKind, |
483 "has_callbacks": mojom.HasCallbacks, | 522 "has_callbacks": mojom.HasCallbacks, |
484 "has_sync_methods": mojom.HasSyncMethods, | 523 "has_sync_methods": mojom.HasSyncMethods, |
485 "requires_context_for_data_view": RequiresContextForDataView, | 524 "requires_context_for_data_view": RequiresContextForDataView, |
486 "should_inline": ShouldInlineStruct, | 525 "should_inline": ShouldInlineStruct, |
487 "should_inline_union": ShouldInlineUnion, | 526 "should_inline_union": ShouldInlineUnion, |
488 "is_array_kind": mojom.IsArrayKind, | 527 "is_array_kind": mojom.IsArrayKind, |
489 "is_enum_kind": mojom.IsEnumKind, | 528 "is_enum_kind": mojom.IsEnumKind, |
490 "is_integral_kind": mojom.IsIntegralKind, | 529 "is_integral_kind": mojom.IsIntegralKind, |
491 "is_native_only_kind": IsNativeOnlyKind, | 530 "is_native_only_kind": IsNativeOnlyKind, |
492 "is_any_handle_kind": mojom.IsAnyHandleKind, | 531 "is_any_handle_kind": mojom.IsAnyHandleKind, |
493 "is_any_interface_kind": mojom.IsAnyInterfaceKind, | 532 "is_any_interface_kind": mojom.IsAnyInterfaceKind, |
494 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, | 533 "is_any_handle_or_interface_kind": mojom.IsAnyHandleOrInterfaceKind, |
495 "is_associated_kind": mojom.IsAssociatedKind, | 534 "is_associated_kind": mojom.IsAssociatedKind, |
| 535 "is_hashable": IsHashableKind, |
496 "is_map_kind": mojom.IsMapKind, | 536 "is_map_kind": mojom.IsMapKind, |
497 "is_nullable_kind": mojom.IsNullableKind, | 537 "is_nullable_kind": mojom.IsNullableKind, |
498 "is_object_kind": mojom.IsObjectKind, | 538 "is_object_kind": mojom.IsObjectKind, |
499 "is_string_kind": mojom.IsStringKind, | 539 "is_string_kind": mojom.IsStringKind, |
500 "is_struct_kind": mojom.IsStructKind, | 540 "is_struct_kind": mojom.IsStructKind, |
501 "is_typemapped_kind": IsTypemappedKind, | 541 "is_typemapped_kind": IsTypemappedKind, |
502 "is_union_kind": mojom.IsUnionKind, | 542 "is_union_kind": mojom.IsUnionKind, |
503 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 543 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
504 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | 544 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, |
505 "stylize_method": generator.StudlyCapsToCamel, | 545 "stylize_method": generator.StudlyCapsToCamel, |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
593 global _use_new_wrapper_types | 633 global _use_new_wrapper_types |
594 _use_new_wrapper_types = self.use_new_wrapper_types | 634 _use_new_wrapper_types = self.use_new_wrapper_types |
595 global _variant | 635 global _variant |
596 _variant = self.variant | 636 _variant = self.variant |
597 suffix = "-%s" % self.variant if self.variant else "" | 637 suffix = "-%s" % self.variant if self.variant else "" |
598 self.Write(self.GenerateModuleHeader(), | 638 self.Write(self.GenerateModuleHeader(), |
599 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 639 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
600 self.Write( | 640 self.Write( |
601 self.GenerateModuleSource(), | 641 self.GenerateModuleSource(), |
602 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 642 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |