| 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 18 matching lines...) Expand all Loading... |
| 29 mojom.UINT16: "U", | 29 mojom.UINT16: "U", |
| 30 mojom.UINT32: "U", | 30 mojom.UINT32: "U", |
| 31 mojom.FLOAT: "f", | 31 mojom.FLOAT: "f", |
| 32 mojom.UINT64: "ULL", | 32 mojom.UINT64: "ULL", |
| 33 } | 33 } |
| 34 | 34 |
| 35 # TODO(rockot): Get rid of these globals. This requires some refactoring of the | 35 # TODO(rockot): Get rid of these globals. This requires some refactoring of the |
| 36 # generator library code so that filters can use the generator as context. | 36 # generator library code so that filters can use the generator as context. |
| 37 _current_typemap = {} | 37 _current_typemap = {} |
| 38 _for_blink = False | 38 _for_blink = False |
| 39 _use_new_wrapper_types = False | |
| 40 # TODO(rockot, yzshen): The variant handling is kind of a hack currently. Make | 39 # TODO(rockot, yzshen): The variant handling is kind of a hack currently. Make |
| 41 # it right. | 40 # it right. |
| 42 _variant = None | 41 _variant = None |
| 43 | 42 |
| 44 | 43 |
| 45 class _NameFormatter(object): | 44 class _NameFormatter(object): |
| 46 """A formatter for the names of kinds or values.""" | 45 """A formatter for the names of kinds or values.""" |
| 47 | 46 |
| 48 def __init__(self, token, variant): | 47 def __init__(self, token, variant): |
| 49 self._token = token | 48 self._token = token |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 134 |
| 136 # TODO(yzshen): Revisit the default value feature. It was designed prior to | 135 # TODO(yzshen): Revisit the default value feature. It was designed prior to |
| 137 # custom type mapping. | 136 # custom type mapping. |
| 138 def DefaultValue(field): | 137 def DefaultValue(field): |
| 139 if field.default: | 138 if field.default: |
| 140 if mojom.IsStructKind(field.kind): | 139 if mojom.IsStructKind(field.kind): |
| 141 assert field.default == "default" | 140 assert field.default == "default" |
| 142 if not IsTypemappedKind(field.kind): | 141 if not IsTypemappedKind(field.kind): |
| 143 return "%s::New()" % GetNameForKind(field.kind) | 142 return "%s::New()" % GetNameForKind(field.kind) |
| 144 return ExpressionToText(field.default, kind=field.kind) | 143 return ExpressionToText(field.default, kind=field.kind) |
| 145 if not _use_new_wrapper_types: | |
| 146 if mojom.IsArrayKind(field.kind) or mojom.IsMapKind(field.kind): | |
| 147 return "nullptr"; | |
| 148 if mojom.IsStringKind(field.kind): | |
| 149 return "" if _for_blink else "nullptr" | |
| 150 return "" | 144 return "" |
| 151 | 145 |
| 152 def NamespaceToArray(namespace): | 146 def NamespaceToArray(namespace): |
| 153 return namespace.split(".") if namespace else [] | 147 return namespace.split(".") if namespace else [] |
| 154 | 148 |
| 155 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, | 149 def GetNameForKind(kind, internal=False, flatten_nested_kind=False, |
| 156 add_same_module_namespaces=False): | 150 add_same_module_namespaces=False): |
| 157 return _NameFormatter(kind, _variant).FormatForCpp( | 151 return _NameFormatter(kind, _variant).FormatForCpp( |
| 158 internal=internal, flatten_nested_kind=flatten_nested_kind, | 152 internal=internal, flatten_nested_kind=flatten_nested_kind, |
| 159 add_same_module_namespaces=add_same_module_namespaces) | 153 add_same_module_namespaces=add_same_module_namespaces) |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 "nullable_is_same_type"]): | 232 "nullable_is_same_type"]): |
| 239 type_name = _AddOptional(type_name) | 233 type_name = _AddOptional(type_name) |
| 240 return type_name | 234 return type_name |
| 241 if mojom.IsEnumKind(kind): | 235 if mojom.IsEnumKind(kind): |
| 242 return GetNameForKind( | 236 return GetNameForKind( |
| 243 kind, add_same_module_namespaces=add_same_module_namespaces) | 237 kind, add_same_module_namespaces=add_same_module_namespaces) |
| 244 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 238 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
| 245 return "%sPtr" % GetNameForKind( | 239 return "%sPtr" % GetNameForKind( |
| 246 kind, add_same_module_namespaces=add_same_module_namespaces) | 240 kind, add_same_module_namespaces=add_same_module_namespaces) |
| 247 if mojom.IsArrayKind(kind): | 241 if mojom.IsArrayKind(kind): |
| 248 pattern = None | 242 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" |
| 249 if _use_new_wrapper_types: | 243 if mojom.IsNullableKind(kind): |
| 250 pattern = "WTF::Vector<%s>" if _for_blink else "std::vector<%s>" | 244 pattern = _AddOptional(pattern) |
| 251 if mojom.IsNullableKind(kind): | |
| 252 pattern = _AddOptional(pattern) | |
| 253 else: | |
| 254 pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>" | |
| 255 return pattern % GetCppWrapperType( | 245 return pattern % GetCppWrapperType( |
| 256 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | 246 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
| 257 if mojom.IsMapKind(kind): | 247 if mojom.IsMapKind(kind): |
| 258 pattern = None | 248 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else |
| 259 if _use_new_wrapper_types: | 249 "std::unordered_map<%s, %s>") |
| 260 pattern = ("WTF::HashMap<%s, %s>" if _for_blink else | 250 if mojom.IsNullableKind(kind): |
| 261 "std::unordered_map<%s, %s>") | 251 pattern = _AddOptional(pattern) |
| 262 if mojom.IsNullableKind(kind): | |
| 263 pattern = _AddOptional(pattern) | |
| 264 else: | |
| 265 pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>" | |
| 266 return pattern % ( | 252 return pattern % ( |
| 267 GetCppWrapperType( | 253 GetCppWrapperType( |
| 268 kind.key_kind, | 254 kind.key_kind, |
| 269 add_same_module_namespaces=add_same_module_namespaces), | 255 add_same_module_namespaces=add_same_module_namespaces), |
| 270 GetCppWrapperType( | 256 GetCppWrapperType( |
| 271 kind.value_kind, | 257 kind.value_kind, |
| 272 add_same_module_namespaces=add_same_module_namespaces)) | 258 add_same_module_namespaces=add_same_module_namespaces)) |
| 273 if mojom.IsInterfaceKind(kind): | 259 if mojom.IsInterfaceKind(kind): |
| 274 return "%sPtr" % GetNameForKind( | 260 return "%sPtr" % GetNameForKind( |
| 275 kind, add_same_module_namespaces=add_same_module_namespaces) | 261 kind, add_same_module_namespaces=add_same_module_namespaces) |
| 276 if mojom.IsInterfaceRequestKind(kind): | 262 if mojom.IsInterfaceRequestKind(kind): |
| 277 return "%sRequest" % GetNameForKind( | 263 return "%sRequest" % GetNameForKind( |
| 278 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | 264 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
| 279 if mojom.IsAssociatedInterfaceKind(kind): | 265 if mojom.IsAssociatedInterfaceKind(kind): |
| 280 return "%sAssociatedPtrInfo" % GetNameForKind( | 266 return "%sAssociatedPtrInfo" % GetNameForKind( |
| 281 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | 267 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
| 282 if mojom.IsAssociatedInterfaceRequestKind(kind): | 268 if mojom.IsAssociatedInterfaceRequestKind(kind): |
| 283 return "%sAssociatedRequest" % GetNameForKind( | 269 return "%sAssociatedRequest" % GetNameForKind( |
| 284 kind.kind, add_same_module_namespaces=add_same_module_namespaces) | 270 kind.kind, add_same_module_namespaces=add_same_module_namespaces) |
| 285 if mojom.IsStringKind(kind): | 271 if mojom.IsStringKind(kind): |
| 286 if _for_blink: | 272 if _for_blink: |
| 287 return "WTF::String" | 273 return "WTF::String" |
| 288 if not _use_new_wrapper_types: | |
| 289 return "mojo::String" | |
| 290 type_name = "std::string" | 274 type_name = "std::string" |
| 291 return _AddOptional(type_name) if mojom.IsNullableKind(kind) else type_name | 275 return _AddOptional(type_name) if mojom.IsNullableKind(kind) else type_name |
| 292 if mojom.IsGenericHandleKind(kind): | 276 if mojom.IsGenericHandleKind(kind): |
| 293 return "mojo::ScopedHandle" | 277 return "mojo::ScopedHandle" |
| 294 if mojom.IsDataPipeConsumerKind(kind): | 278 if mojom.IsDataPipeConsumerKind(kind): |
| 295 return "mojo::ScopedDataPipeConsumerHandle" | 279 return "mojo::ScopedDataPipeConsumerHandle" |
| 296 if mojom.IsDataPipeProducerKind(kind): | 280 if mojom.IsDataPipeProducerKind(kind): |
| 297 return "mojo::ScopedDataPipeProducerHandle" | 281 return "mojo::ScopedDataPipeProducerHandle" |
| 298 if mojom.IsMessagePipeKind(kind): | 282 if mojom.IsMessagePipeKind(kind): |
| 299 return "mojo::ScopedMessagePipeHandle" | 283 return "mojo::ScopedMessagePipeHandle" |
| 300 if mojom.IsSharedBufferKind(kind): | 284 if mojom.IsSharedBufferKind(kind): |
| 301 return "mojo::ScopedSharedBufferHandle" | 285 return "mojo::ScopedSharedBufferHandle" |
| 302 if not kind in _kind_to_cpp_type: | 286 if not kind in _kind_to_cpp_type: |
| 303 raise Exception("Unrecognized kind %s" % kind.spec) | 287 raise Exception("Unrecognized kind %s" % kind.spec) |
| 304 return _kind_to_cpp_type[kind] | 288 return _kind_to_cpp_type[kind] |
| 305 | 289 |
| 306 def IsMoveOnlyKind(kind): | 290 def IsMoveOnlyKind(kind): |
| 307 if IsTypemappedKind(kind): | 291 if IsTypemappedKind(kind): |
| 308 if mojom.IsEnumKind(kind): | 292 if mojom.IsEnumKind(kind): |
| 309 return False | 293 return False |
| 310 return _current_typemap[GetFullMojomNameForKind(kind)]["move_only"] | 294 return _current_typemap[GetFullMojomNameForKind(kind)]["move_only"] |
| 311 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): | 295 if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): |
| 312 return True | 296 return True |
| 313 if mojom.IsArrayKind(kind): | 297 if mojom.IsArrayKind(kind): |
| 314 return IsMoveOnlyKind(kind.kind) if _use_new_wrapper_types else True | 298 return IsMoveOnlyKind(kind.kind) |
| 315 if mojom.IsMapKind(kind): | 299 if mojom.IsMapKind(kind): |
| 316 return IsMoveOnlyKind(kind.value_kind) if _use_new_wrapper_types else True | 300 return IsMoveOnlyKind(kind.value_kind) |
| 317 if mojom.IsAnyHandleOrInterfaceKind(kind): | 301 if mojom.IsAnyHandleOrInterfaceKind(kind): |
| 318 return True | 302 return True |
| 319 return False | 303 return False |
| 320 | 304 |
| 321 def IsCopyablePassByValue(kind): | 305 def IsCopyablePassByValue(kind): |
| 322 if not IsTypemappedKind(kind): | 306 if not IsTypemappedKind(kind): |
| 323 return False | 307 return False |
| 324 return _current_typemap[GetFullMojomNameForKind(kind)][ | 308 return _current_typemap[GetFullMojomNameForKind(kind)][ |
| 325 "copyable_pass_by_value"] | 309 "copyable_pass_by_value"] |
| 326 | 310 |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 "kinds": self.module.kinds, | 598 "kinds": self.module.kinds, |
| 615 "enums": self.module.enums, | 599 "enums": self.module.enums, |
| 616 "all_enums": all_enums, | 600 "all_enums": all_enums, |
| 617 "structs": structs, | 601 "structs": structs, |
| 618 "unions": self.GetUnions(), | 602 "unions": self.GetUnions(), |
| 619 "interfaces": interfaces, | 603 "interfaces": interfaces, |
| 620 "variant": self.variant, | 604 "variant": self.variant, |
| 621 "extra_traits_headers": self.GetExtraTraitsHeaders(), | 605 "extra_traits_headers": self.GetExtraTraitsHeaders(), |
| 622 "extra_public_headers": self.GetExtraPublicHeaders(), | 606 "extra_public_headers": self.GetExtraPublicHeaders(), |
| 623 "for_blink": self.for_blink, | 607 "for_blink": self.for_blink, |
| 624 "use_new_wrapper_types": self.use_new_wrapper_types, | |
| 625 "use_once_callback": self.use_once_callback, | 608 "use_once_callback": self.use_once_callback, |
| 626 "export_attribute": self.export_attribute, | 609 "export_attribute": self.export_attribute, |
| 627 "export_header": self.export_header, | 610 "export_header": self.export_header, |
| 628 } | 611 } |
| 629 | 612 |
| 630 @staticmethod | 613 @staticmethod |
| 631 def GetTemplatePrefix(): | 614 def GetTemplatePrefix(): |
| 632 return "cpp_templates" | 615 return "cpp_templates" |
| 633 | 616 |
| 634 @classmethod | 617 @classmethod |
| (...skipping 27 matching lines...) Expand all Loading... |
| 662 self.Write( | 645 self.Write( |
| 663 self.GenerateModuleSharedInternalHeader(), | 646 self.GenerateModuleSharedInternalHeader(), |
| 664 self.MatchMojomFilePath("%s-shared-internal.h" % self.module.name)) | 647 self.MatchMojomFilePath("%s-shared-internal.h" % self.module.name)) |
| 665 self.Write(self.GenerateModuleSharedSource(), | 648 self.Write(self.GenerateModuleSharedSource(), |
| 666 self.MatchMojomFilePath("%s-shared.cc" % self.module.name)) | 649 self.MatchMojomFilePath("%s-shared.cc" % self.module.name)) |
| 667 else: | 650 else: |
| 668 global _current_typemap | 651 global _current_typemap |
| 669 _current_typemap = self.typemap | 652 _current_typemap = self.typemap |
| 670 global _for_blink | 653 global _for_blink |
| 671 _for_blink = self.for_blink | 654 _for_blink = self.for_blink |
| 672 global _use_new_wrapper_types | |
| 673 _use_new_wrapper_types = self.use_new_wrapper_types | |
| 674 global _use_once_callback | 655 global _use_once_callback |
| 675 _use_once_callback = self.use_once_callback | 656 _use_once_callback = self.use_once_callback |
| 676 global _variant | 657 global _variant |
| 677 _variant = self.variant | 658 _variant = self.variant |
| 678 suffix = "-%s" % self.variant if self.variant else "" | 659 suffix = "-%s" % self.variant if self.variant else "" |
| 679 self.Write(self.GenerateModuleHeader(), | 660 self.Write(self.GenerateModuleHeader(), |
| 680 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) | 661 self.MatchMojomFilePath("%s%s.h" % (self.module.name, suffix))) |
| 681 self.Write( | 662 self.Write( |
| 682 self.GenerateModuleSource(), | 663 self.GenerateModuleSource(), |
| 683 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) | 664 self.MatchMojomFilePath("%s%s.cc" % (self.module.name, suffix))) |
| OLD | NEW |