| 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 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 | 145 |
| 146 def NamespaceToArray(namespace): | 146 def NamespaceToArray(namespace): |
| 147 return namespace.split(".") if namespace else [] | 147 return namespace.split(".") if namespace else [] |
| 148 | 148 |
| 149 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, | 149 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, |
| 150 add_same_module_namespaces=False): | 150 add_same_module_namespaces=False): |
| 151 return _NameFormatter(kind, _variant).FormatForCpp( | 151 return _NameFormatter(kind, _variant).FormatForCpp( |
| 152 internal=internal, flatten_nested_kind=flatten_nested_kind, | 152 internal=internal, flatten_nested_kind=flatten_nested_kind, |
| 153 add_same_module_namespaces=add_same_module_namespaces) | 153 add_same_module_namespaces=add_same_module_namespaces) |
| 154 | 154 |
| 155 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False): | 155 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False, |
| 156 return _NameFormatter(kind, _variant).FormatForCpp( | 156 include_variant=True): |
| 157 internal=internal, add_same_module_namespaces=True, | 157 return _NameFormatter( |
| 158 flatten_nested_kind=flatten_nested_kind) | 158 kind, _variant if include_variant else None).FormatForCpp( |
| 159 internal=internal, add_same_module_namespaces=True, |
| 160 flatten_nested_kind=flatten_nested_kind) |
| 161 |
| 162 |
| 163 def GetWtfHashFnNameForEnum(enum): |
| 164 return _NameFormatter( |
| 165 enum, None).Format("_", internal=True, add_same_module_namespaces=True, |
| 166 flatten_nested_kind=True) + "HashFn" |
| 167 |
| 159 | 168 |
| 160 def GetFullMojomNameForKind(kind): | 169 def GetFullMojomNameForKind(kind): |
| 161 return _NameFormatter(kind, _variant).FormatForMojom() | 170 return _NameFormatter(kind, _variant).FormatForMojom() |
| 162 | 171 |
| 163 def IsTypemappedKind(kind): | 172 def IsTypemappedKind(kind): |
| 164 return hasattr(kind, "name") and \ | 173 return hasattr(kind, "name") and \ |
| 165 GetFullMojomNameForKind(kind) in _current_typemap | 174 GetFullMojomNameForKind(kind) in _current_typemap |
| 166 | 175 |
| 167 def IsNativeOnlyKind(kind): | 176 def IsNativeOnlyKind(kind): |
| 168 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ | 177 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 # use case yet. | 212 # use case yet. |
| 204 elif mojom.IsArrayKind(kind): | 213 elif mojom.IsArrayKind(kind): |
| 205 return False | 214 return False |
| 206 elif mojom.IsMapKind(kind): | 215 elif mojom.IsMapKind(kind): |
| 207 return False | 216 return False |
| 208 else: | 217 else: |
| 209 return True | 218 return True |
| 210 return Check(kind) | 219 return Check(kind) |
| 211 | 220 |
| 212 | 221 |
| 222 def AllEnumValues(enum): |
| 223 """Return all enum values associated with an enum. |
| 224 |
| 225 Args: |
| 226 enum: {mojom.Enum} The enum type. |
| 227 |
| 228 Returns: |
| 229 {Set[int]} The values. |
| 230 """ |
| 231 return set(field.numeric_value for field in enum.fields) |
| 232 |
| 233 |
| 213 def GetNativeTypeName(typemapped_kind): | 234 def GetNativeTypeName(typemapped_kind): |
| 214 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] | 235 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] |
| 215 | 236 |
| 216 def GetCppPodType(kind): | 237 def GetCppPodType(kind): |
| 217 return _kind_to_cpp_type[kind] | 238 return _kind_to_cpp_type[kind] |
| 218 | 239 |
| 219 def FormatConstantDeclaration(constant, nested=False): | 240 def FormatConstantDeclaration(constant, nested=False): |
| 220 if mojom.IsStringKind(constant.kind): | 241 if mojom.IsStringKind(constant.kind): |
| 221 if nested: | 242 if nested: |
| 222 return "const char %s[]" % constant.name | 243 return "const char %s[]" % constant.name |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and | 544 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and |
| 524 not mojom.IsStringKind(kind)): | 545 not mojom.IsStringKind(kind)): |
| 525 return "nullptr" | 546 return "nullptr" |
| 526 | 547 |
| 527 return "new mojo::internal::ContainerValidateParams(%s)" % ( | 548 return "new mojo::internal::ContainerValidateParams(%s)" % ( |
| 528 GetContainerValidateParamsCtorArgs(kind)) | 549 GetContainerValidateParamsCtorArgs(kind)) |
| 529 | 550 |
| 530 class Generator(generator.Generator): | 551 class Generator(generator.Generator): |
| 531 | 552 |
| 532 cpp_filters = { | 553 cpp_filters = { |
| 554 "all_enum_values": AllEnumValues, |
| 533 "constant_value": ConstantValue, | 555 "constant_value": ConstantValue, |
| 534 "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces, | 556 "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces, |
| 535 "contains_move_only_members": ContainsMoveOnlyMembers, | 557 "contains_move_only_members": ContainsMoveOnlyMembers, |
| 536 "cpp_wrapper_param_type": GetCppWrapperParamType, | 558 "cpp_wrapper_param_type": GetCppWrapperParamType, |
| 537 "cpp_data_view_type": GetCppDataViewType, | 559 "cpp_data_view_type": GetCppDataViewType, |
| 538 "cpp_field_type": GetCppFieldType, | 560 "cpp_field_type": GetCppFieldType, |
| 539 "cpp_union_field_type": GetCppUnionFieldType, | 561 "cpp_union_field_type": GetCppUnionFieldType, |
| 540 "cpp_pod_type": GetCppPodType, | 562 "cpp_pod_type": GetCppPodType, |
| 541 "cpp_union_getter_return_type": GetUnionGetterReturnType, | 563 "cpp_union_getter_return_type": GetUnionGetterReturnType, |
| 542 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType, | 564 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 568 "is_object_kind": mojom.IsObjectKind, | 590 "is_object_kind": mojom.IsObjectKind, |
| 569 "is_reference_kind": mojom.IsReferenceKind, | 591 "is_reference_kind": mojom.IsReferenceKind, |
| 570 "is_string_kind": mojom.IsStringKind, | 592 "is_string_kind": mojom.IsStringKind, |
| 571 "is_struct_kind": mojom.IsStructKind, | 593 "is_struct_kind": mojom.IsStructKind, |
| 572 "is_typemapped_kind": IsTypemappedKind, | 594 "is_typemapped_kind": IsTypemappedKind, |
| 573 "is_union_kind": mojom.IsUnionKind, | 595 "is_union_kind": mojom.IsUnionKind, |
| 574 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 596 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
| 575 "stylize_method": generator.StudlyCapsToCamel, | 597 "stylize_method": generator.StudlyCapsToCamel, |
| 576 "under_to_camel": generator.UnderToCamel, | 598 "under_to_camel": generator.UnderToCamel, |
| 577 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, | 599 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, |
| 600 "wtf_hash_fn_name_for_enum": GetWtfHashFnNameForEnum, |
| 578 } | 601 } |
| 579 | 602 |
| 580 def GetExtraTraitsHeaders(self): | 603 def GetExtraTraitsHeaders(self): |
| 581 extra_headers = set() | 604 extra_headers = set() |
| 582 for typemap in self._GetAllUsedTypemaps(): | 605 for typemap in self._GetAllUsedTypemaps(): |
| 583 extra_headers.update(typemap.get("traits_headers", [])) | 606 extra_headers.update(typemap.get("traits_headers", [])) |
| 584 return sorted(extra_headers) | 607 return sorted(extra_headers) |
| 585 | 608 |
| 586 def _GetAllUsedTypemaps(self): | 609 def _GetAllUsedTypemaps(self): |
| 587 """Returns the typemaps for types needed for serialization in this module. | 610 """Returns the typemaps for types needed for serialization in this module. |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 729 global _use_once_callback | 752 global _use_once_callback |
| 730 _use_once_callback = self.use_once_callback | 753 _use_once_callback = self.use_once_callback |
| 731 global _variant | 754 global _variant |
| 732 _variant = self.variant | 755 _variant = self.variant |
| 733 suffix = "-%s" % self.variant if self.variant else "" | 756 suffix = "-%s" % self.variant if self.variant else "" |
| 734 self.Write(self.GenerateModuleHeader(), | 757 self.Write(self.GenerateModuleHeader(), |
| 735 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 758 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
| 736 self.Write( | 759 self.Write( |
| 737 self.GenerateModuleSource(), | 760 self.GenerateModuleSource(), |
| 738 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 761 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
| OLD | NEW |