Chromium Code Reviews| 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 return True | 184 return True |
| 185 checked.add(kind.spec) | 185 checked.add(kind.spec) |
| 186 if mojom.IsNullableKind(kind): | 186 if mojom.IsNullableKind(kind): |
| 187 return False | 187 return False |
| 188 elif mojom.IsStructKind(kind): | 188 elif mojom.IsStructKind(kind): |
| 189 if (IsTypemappedKind(kind) and | 189 if (IsTypemappedKind(kind) and |
| 190 not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"]): | 190 not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"]): |
| 191 return False | 191 return False |
| 192 return all(Check(field.kind) for field in kind.fields) | 192 return all(Check(field.kind) for field in kind.fields) |
| 193 elif mojom.IsEnumKind(kind): | 193 elif mojom.IsEnumKind(kind): |
| 194 if (IsTypemappedKind(kind) and | 194 return not IsTypemappedKind(kind) or _current_typemap[ |
| 195 not _current_typemap[GetFullMojomNameForKind(kind)]["hashable"]): | 195 GetFullMojomNameForKind(kind)]["hashable"] |
| 196 return False | |
| 197 return True | |
| 198 elif mojom.IsUnionKind(kind): | 196 elif mojom.IsUnionKind(kind): |
| 199 return all(Check(field.kind) for field in kind.fields) | 197 return all(Check(field.kind) for field in kind.fields) |
| 200 elif mojom.IsAnyHandleKind(kind): | 198 elif mojom.IsAnyHandleKind(kind): |
| 201 return False | 199 return False |
| 202 elif mojom.IsAnyInterfaceKind(kind): | 200 elif mojom.IsAnyInterfaceKind(kind): |
| 203 return False | 201 return False |
| 204 # TODO(tibell): Arrays and maps could be made hashable. We just don't have a | 202 # TODO(tibell): Arrays and maps could be made hashable. We just don't have a |
| 205 # use case yet. | 203 # use case yet. |
| 206 elif mojom.IsArrayKind(kind): | 204 elif mojom.IsArrayKind(kind): |
| 207 return False | 205 return False |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 return "mojo::ScopedMessagePipeHandle" | 410 return "mojo::ScopedMessagePipeHandle" |
| 413 if mojom.IsSharedBufferKind(kind): | 411 if mojom.IsSharedBufferKind(kind): |
| 414 return "mojo::ScopedSharedBufferHandle" | 412 return "mojo::ScopedSharedBufferHandle" |
| 415 return _kind_to_cpp_type[kind] | 413 return _kind_to_cpp_type[kind] |
| 416 | 414 |
| 417 def GetUnmappedTypeForSerializer(kind): | 415 def GetUnmappedTypeForSerializer(kind): |
| 418 return GetCppDataViewType(kind, qualified=True) | 416 return GetCppDataViewType(kind, qualified=True) |
| 419 | 417 |
| 420 def TranslateConstants(token, kind): | 418 def TranslateConstants(token, kind): |
| 421 if isinstance(token, mojom.NamedValue): | 419 if isinstance(token, mojom.NamedValue): |
| 422 return _NameFormatter(token, _variant).FormatForCpp( | 420 return GetNameForKind(token, flatten_nested_kind=True) |
| 423 flatten_nested_kind=True) | |
| 424 | 421 |
| 425 if isinstance(token, mojom.BuiltinValue): | 422 if isinstance(token, mojom.BuiltinValue): |
| 426 if token.value == "double.INFINITY": | 423 if token.value == "double.INFINITY": |
| 427 return "std::numeric_limits<double>::infinity()" | 424 return "std::numeric_limits<double>::infinity()" |
| 428 if token.value == "float.INFINITY": | 425 if token.value == "float.INFINITY": |
| 429 return "std::numeric_limits<float>::infinity()" | 426 return "std::numeric_limits<float>::infinity()" |
| 430 if token.value == "double.NEGATIVE_INFINITY": | 427 if token.value == "double.NEGATIVE_INFINITY": |
| 431 return "-std::numeric_limits<double>::infinity()" | 428 return "-std::numeric_limits<double>::infinity()" |
| 432 if token.value == "float.NEGATIVE_INFINITY": | 429 if token.value == "float.NEGATIVE_INFINITY": |
| 433 return "-std::numeric_limits<float>::infinity()" | 430 return "-std::numeric_limits<float>::infinity()" |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 568 "is_hashable": IsHashableKind, | 565 "is_hashable": IsHashableKind, |
| 569 "is_map_kind": mojom.IsMapKind, | 566 "is_map_kind": mojom.IsMapKind, |
| 570 "is_nullable_kind": mojom.IsNullableKind, | 567 "is_nullable_kind": mojom.IsNullableKind, |
| 571 "is_object_kind": mojom.IsObjectKind, | 568 "is_object_kind": mojom.IsObjectKind, |
| 572 "is_reference_kind": mojom.IsReferenceKind, | 569 "is_reference_kind": mojom.IsReferenceKind, |
| 573 "is_string_kind": mojom.IsStringKind, | 570 "is_string_kind": mojom.IsStringKind, |
| 574 "is_struct_kind": mojom.IsStructKind, | 571 "is_struct_kind": mojom.IsStructKind, |
| 575 "is_typemapped_kind": IsTypemappedKind, | 572 "is_typemapped_kind": IsTypemappedKind, |
| 576 "is_union_kind": mojom.IsUnionKind, | 573 "is_union_kind": mojom.IsUnionKind, |
| 577 "passes_associated_kinds": mojom.PassesAssociatedKinds, | 574 "passes_associated_kinds": mojom.PassesAssociatedKinds, |
| 578 "struct_size": lambda ps: ps.GetTotalSize() + _HEADER_SIZE, | |
| 579 "stylize_method": generator.StudlyCapsToCamel, | 575 "stylize_method": generator.StudlyCapsToCamel, |
| 580 "under_to_camel": generator.UnderToCamel, | 576 "under_to_camel": generator.UnderToCamel, |
| 581 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, | 577 "unmapped_type_for_serializer": GetUnmappedTypeForSerializer, |
| 582 } | 578 } |
| 583 | 579 |
| 584 def GetExtraTraitsHeaders(self): | 580 def GetExtraTraitsHeaders(self): |
| 585 extra_headers = set() | 581 extra_headers = set() |
| 586 for entry in self.typemap.itervalues(): | 582 for typemap in self._GetAllUsedTypemaps(): |
| 587 extra_headers.update(entry.get("traits_headers", [])) | 583 extra_headers.update(typemap.get("traits_headers", [])) |
| 588 return list(extra_headers) | 584 return sorted(extra_headers) |
| 585 | |
| 586 def _GetAllUsedTypemaps(self): | |
| 587 """Returns the typemaps for types needed for serialization in this module. | |
| 588 | |
| 589 A type is needed for serialization if it is contained by a struct or enum | |
|
yzshen1
2017/02/07 00:21:45
enum -> union?
Sam McNally
2017/02/07 01:48:31
Done.
| |
| 590 defined in this module, is a parameter of a message in an interface in | |
| 591 this module or is contained within another type needed for serialization. | |
| 592 """ | |
| 593 used_typemaps = [] | |
| 594 seen_types = set() | |
| 595 def AddKind(kind): | |
| 596 if (mojom.IsIntegralKind(kind) or mojom.IsStringKind(kind) or | |
| 597 mojom.IsDoubleKind(kind) or mojom.IsFloatKind(kind) or | |
| 598 mojom.IsAnyHandleKind(kind) or | |
| 599 mojom.IsInterfaceKind(kind) or | |
| 600 mojom.IsInterfaceRequestKind(kind) or | |
| 601 mojom.IsAssociatedKind(kind)): | |
| 602 pass | |
| 603 elif mojom.IsArrayKind(kind): | |
| 604 AddKind(kind.kind) | |
| 605 elif mojom.IsMapKind(kind): | |
| 606 AddKind(kind.key_kind) | |
| 607 AddKind(kind.value_kind) | |
| 608 else: | |
| 609 name = GetFullMojomNameForKind(kind) | |
| 610 if name in seen_types: | |
| 611 return | |
| 612 seen_types.add(name) | |
| 613 | |
| 614 typemap = _current_typemap.get(name, None) | |
| 615 if typemap: | |
| 616 used_typemaps.append(typemap) | |
| 617 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | |
| 618 for field in kind.fields: | |
| 619 AddKind(field.kind) | |
| 620 | |
| 621 for kind in self.module.structs + self.module.unions: | |
| 622 for field in kind.fields: | |
| 623 AddKind(field.kind) | |
| 624 | |
| 625 for interface in self.module.interfaces: | |
| 626 for method in interface.methods: | |
| 627 for parameter in method.parameters + (method.response_parameters or []): | |
| 628 AddKind(parameter.kind) | |
| 629 | |
| 630 return used_typemaps | |
| 589 | 631 |
| 590 def GetExtraPublicHeaders(self): | 632 def GetExtraPublicHeaders(self): |
| 591 extra_headers = set() | 633 all_enums = list(self.module.enums) |
| 592 for entry in self.typemap.itervalues(): | 634 for struct in self.module.structs: |
| 593 extra_headers.update(entry.get("public_headers", [])) | 635 all_enums.extend(struct.enums) |
| 594 return list(extra_headers) | 636 for interface in self.module.interfaces: |
| 637 all_enums.extend(interface.enums) | |
| 638 | |
| 639 types = set(GetFullMojomNameForKind(typename) | |
| 640 for typename in | |
| 641 self.module.structs + all_enums + self.module.unions) | |
| 642 headers = set() | |
| 643 for typename, typemap in self.typemap.iteritems(): | |
| 644 if typename in types: | |
| 645 headers.update(typemap.get("public_headers", [])) | |
| 646 return sorted(headers) | |
| 647 | |
| 648 def _GetDirectlyUsedKinds(self): | |
| 649 for struct in self.module.structs + self.module.unions: | |
| 650 for field in struct.fields: | |
| 651 yield field.kind | |
| 652 | |
| 653 for interface in self.module.interfaces: | |
| 654 for method in interface.methods: | |
| 655 for param in method.parameters + (method.response_parameters or []): | |
| 656 yield param.kind | |
| 595 | 657 |
| 596 def GetJinjaExports(self): | 658 def GetJinjaExports(self): |
| 597 structs = self.GetStructs() | 659 structs = self.GetStructs() |
| 598 interfaces = self.GetInterfaces() | 660 interfaces = self.GetInterfaces() |
| 599 all_enums = list(self.module.enums) | 661 all_enums = list(self.module.enums) |
| 600 for struct in structs: | 662 for struct in structs: |
| 601 all_enums.extend(struct.enums) | 663 all_enums.extend(struct.enums) |
| 602 for interface in interfaces: | 664 for interface in interfaces: |
| 603 all_enums.extend(interface.enums) | 665 all_enums.extend(interface.enums) |
| 604 | 666 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 667 global _use_once_callback | 729 global _use_once_callback |
| 668 _use_once_callback = self.use_once_callback | 730 _use_once_callback = self.use_once_callback |
| 669 global _variant | 731 global _variant |
| 670 _variant = self.variant | 732 _variant = self.variant |
| 671 suffix = "-%s" % self.variant if self.variant else "" | 733 suffix = "-%s" % self.variant if self.variant else "" |
| 672 self.Write(self.GenerateModuleHeader(), | 734 self.Write(self.GenerateModuleHeader(), |
| 673 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 735 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
| 674 self.Write( | 736 self.Write( |
| 675 self.GenerateModuleSource(), | 737 self.GenerateModuleSource(), |
| 676 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 738 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
| OLD | NEW |