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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 146 |
147 def NamespaceToArray(namespace): | 147 def NamespaceToArray(namespace): |
148 return namespace.split(".") if namespace else [] | 148 return namespace.split(".") if namespace else [] |
149 | 149 |
150 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, | 150 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, |
151 add_same_module_namespaces=False): | 151 add_same_module_namespaces=False): |
152 return _NameFormatter(kind, _variant).FormatForCpp( | 152 return _NameFormatter(kind, _variant).FormatForCpp( |
153 internal=internal, flatten_nested_kind=flatten_nested_kind, | 153 internal=internal, flatten_nested_kind=flatten_nested_kind, |
154 add_same_module_namespaces=add_same_module_namespaces) | 154 add_same_module_namespaces=add_same_module_namespaces) |
155 | 155 |
156 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False): | 156 def GetQualifiedNameForKind(kind, internal=False, flatten_nested_kind=False, |
157 return _NameFormatter(kind, _variant).FormatForCpp( | 157 include_variant=True): |
158 internal=internal, add_same_module_namespaces=True, | 158 return _NameFormatter( |
159 flatten_nested_kind=flatten_nested_kind) | 159 kind, _variant if include_variant else None).FormatForCpp( |
| 160 internal=internal, add_same_module_namespaces=True, |
| 161 flatten_nested_kind=flatten_nested_kind) |
| 162 |
| 163 |
| 164 def GetWtfHashFnNameForEnum(enum): |
| 165 return _NameFormatter( |
| 166 enum, None).Format("_", internal=True, add_same_module_namespaces=True, |
| 167 flatten_nested_kind=True) + "HashFn" |
| 168 |
160 | 169 |
161 def GetFullMojomNameForKind(kind): | 170 def GetFullMojomNameForKind(kind): |
162 return _NameFormatter(kind, _variant).FormatForMojom() | 171 return _NameFormatter(kind, _variant).FormatForMojom() |
163 | 172 |
164 def IsTypemappedKind(kind): | 173 def IsTypemappedKind(kind): |
165 return hasattr(kind, "name") and \ | 174 return hasattr(kind, "name") and \ |
166 GetFullMojomNameForKind(kind) in _current_typemap | 175 GetFullMojomNameForKind(kind) in _current_typemap |
167 | 176 |
168 def IsNativeOnlyKind(kind): | 177 def IsNativeOnlyKind(kind): |
169 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ | 178 return (mojom.IsStructKind(kind) or mojom.IsEnumKind(kind)) and \ |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 # use case yet. | 213 # use case yet. |
205 elif mojom.IsArrayKind(kind): | 214 elif mojom.IsArrayKind(kind): |
206 return False | 215 return False |
207 elif mojom.IsMapKind(kind): | 216 elif mojom.IsMapKind(kind): |
208 return False | 217 return False |
209 else: | 218 else: |
210 return True | 219 return True |
211 return Check(kind) | 220 return Check(kind) |
212 | 221 |
213 | 222 |
| 223 def AllEnumValues(enum): |
| 224 """Return all enum values associated with an enum. |
| 225 |
| 226 Args: |
| 227 enum: {mojom.Enum} The enum type. |
| 228 |
| 229 Returns: |
| 230 {Set[int]} The values. |
| 231 """ |
| 232 return set(field.numeric_value for field in enum.fields) |
| 233 |
| 234 |
214 def GetNativeTypeName(typemapped_kind): | 235 def GetNativeTypeName(typemapped_kind): |
215 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] | 236 return _current_typemap[GetFullMojomNameForKind(typemapped_kind)]["typename"] |
216 | 237 |
217 def GetCppPodType(kind): | 238 def GetCppPodType(kind): |
218 return _kind_to_cpp_type[kind] | 239 return _kind_to_cpp_type[kind] |
219 | 240 |
220 def FormatConstantDeclaration(constant, nested=False): | 241 def FormatConstantDeclaration(constant, nested=False): |
221 if mojom.IsStringKind(constant.kind): | 242 if mojom.IsStringKind(constant.kind): |
222 if nested: | 243 if nested: |
223 return "const char %s[]" % constant.name | 244 return "const char %s[]" % constant.name |
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and | 598 if (not mojom.IsArrayKind(kind) and not mojom.IsMapKind(kind) and |
578 not mojom.IsStringKind(kind)): | 599 not mojom.IsStringKind(kind)): |
579 return "nullptr" | 600 return "nullptr" |
580 | 601 |
581 return "new mojo::internal::ContainerValidateParams(%s)" % ( | 602 return "new mojo::internal::ContainerValidateParams(%s)" % ( |
582 GetContainerValidateParamsCtorArgs(kind)) | 603 GetContainerValidateParamsCtorArgs(kind)) |
583 | 604 |
584 class Generator(generator.Generator): | 605 class Generator(generator.Generator): |
585 | 606 |
586 cpp_filters = { | 607 cpp_filters = { |
| 608 "all_enum_values": AllEnumValues, |
587 "constant_value": ConstantValue, | 609 "constant_value": ConstantValue, |
588 "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces, | 610 "contains_handles_or_interfaces": mojom.ContainsHandlesOrInterfaces, |
589 "contains_move_only_members": ContainsMoveOnlyMembers, | 611 "contains_move_only_members": ContainsMoveOnlyMembers, |
590 "cpp_wrapper_param_type": GetCppWrapperParamType, | 612 "cpp_wrapper_param_type": GetCppWrapperParamType, |
591 "cpp_data_view_type": GetCppDataViewType, | 613 "cpp_data_view_type": GetCppDataViewType, |
592 "cpp_field_type": GetCppFieldType, | 614 "cpp_field_type": GetCppFieldType, |
593 "cpp_union_field_type": GetCppUnionFieldType, | 615 "cpp_union_field_type": GetCppUnionFieldType, |
594 "cpp_pod_type": GetCppPodType, | 616 "cpp_pod_type": GetCppPodType, |
595 "cpp_union_getter_return_type": GetUnionGetterReturnType, | 617 "cpp_union_getter_return_type": GetUnionGetterReturnType, |
596 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType, | 618 "cpp_union_trait_getter_return_type": GetUnionTraitGetterReturnType, |
(...skipping 26 matching lines...) Expand all Loading... |
623 "is_reference_kind": mojom.IsReferenceKind, | 645 "is_reference_kind": mojom.IsReferenceKind, |
624 "is_string_kind": mojom.IsStringKind, | 646 "is_string_kind": mojom.IsStringKind, |
625 "is_struct_kind": mojom.IsStructKind, | 647 "is_struct_kind": mojom.IsStructKind, |
626 "is_typemapped_kind": IsTypemappedKind, | 648 "is_typemapped_kind": IsTypemappedKind, |
627 "is_union_kind": mojom.IsUnionKind, | 649 "is_union_kind": mojom.IsUnionKind, |
628 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 650 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
629 "struct_constructors": GetStructConstructors, | 651 "struct_constructors": GetStructConstructors, |
630 "stylize_method": generator.StudlyCapsToCamel, | 652 "stylize_method": generator.StudlyCapsToCamel, |
631 "under_to_camel": generator.UnderToCamel, | 653 "under_to_camel": generator.UnderToCamel, |
632 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, | 654 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, |
| 655 "wtf_hash_fn_name_for_enum": GetWtfHashFnNameForEnum, |
633 } | 656 } |
634 | 657 |
635 def GetExtraTraitsHeaders(self): | 658 def GetExtraTraitsHeaders(self): |
636 extra_headers = set() | 659 extra_headers = set() |
637 for typemap in self._GetAllUsedTypemaps(): | 660 for typemap in self._GetAllUsedTypemaps(): |
638 extra_headers.update(typemap.get("traits_headers", [])) | 661 extra_headers.update(typemap.get("traits_headers", [])) |
639 return sorted(extra_headers) | 662 return sorted(extra_headers) |
640 | 663 |
641 def _GetAllUsedTypemaps(self): | 664 def _GetAllUsedTypemaps(self): |
642 """Returns the typemaps for types needed for serialization in this module. | 665 """Returns the typemaps for types needed for serialization in this module. |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
786 global _variant | 809 global _variant |
787 _variant = self.variant | 810 _variant = self.variant |
788 global _export_attribute | 811 global _export_attribute |
789 _export_attribute = self.export_attribute | 812 _export_attribute = self.export_attribute |
790 suffix = "-%s" % self.variant if self.variant else "" | 813 suffix = "-%s" % self.variant if self.variant else "" |
791 self.Write(self.GenerateModuleHeader(), | 814 self.Write(self.GenerateModuleHeader(), |
792 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 815 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
793 self.Write( | 816 self.Write( |
794 self.GenerateModuleSource(), | 817 self.GenerateModuleSource(), |
795 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 818 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
OLD | NEW |